package com.wuliupai.delivery.wuliupaidelivery.utils;

import android.content.Context;
import android.os.CountDownTimer;
import android.widget.Button;

import com.wuliupai.delivery.wuliupaidelivery.R;


/**
 * 倒计时
 *
 * @author KangHuiLi
 */

public class TimeCountDownUtil extends CountDownTimer {

    private Button btn_sendCode;
    private Context mContext;
    private int btnTextRes;
    private boolean notChangeBtnBackground = false;

    public TimeCountDownUtil(Context context, long millisInFuture, long countDownInterval,
                             Button btn_sendCode, int btnTextRes) {
        super(millisInFuture, countDownInterval);
        this.btn_sendCode = btn_sendCode;
        this.mContext = context;
        this.btnTextRes = btnTextRes;
    }

    public TimeCountDownUtil(Context context, long millisInFuture, long countDownInterval,
                             Button btn_sendCode, int btnTextRes, boolean notChangeBtnBackground) {
        super(millisInFuture, countDownInterval);
        this.btn_sendCode = btn_sendCode;
        this.mContext = context;
        this.btnTextRes = btnTextRes;
        this.notChangeBtnBackground = notChangeBtnBackground;
    }

    public TimeCountDownUtil(Context context, long millisInFuture, long countDownInterval,
                             Button btn_sendCode) {
        super(millisInFuture, countDownInterval);
        this.btn_sendCode = btn_sendCode;
        this.mContext = context;
        this.btnTextRes = R.string.strings_login_code_again;
    }

    // 倒计时结束
    @Override
    public void onFinish() {
        btn_sendCode.setText(btnTextRes);
        if (!notChangeBtnBackground) {
            btn_sendCode.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.comm_press_selector));
        }
        btn_sendCode.setClickable(true);
    }

    // 倒计时进行中
    @Override
    public void onTick(long millisUntilFinished) {
        btn_sendCode.setClickable(false);
        if (!notChangeBtnBackground) {
            btn_sendCode.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.comm_not_press));
        }
        btn_sendCode.setText(millisUntilFinished / 1000 + "s");
    }

}
